import axios from 'axios'
import router from '@/router'
import qs from "qs"
import { Message } from 'element-ui'
// import { encryptAES, decryptAES, generatekeyAES } from '@/assets/js/commonAse'
// import { decode } from '@/assets/js/utils/Base64.js'
// const baseURL = 'http://192.168.0.2:8085/sjtulab/'
const baseURL = 'http://10.119.1.233:8080/sjtulab/' //测试环境
// const baseURL = 'http://10.119.9.72:8080/sjtulab/'  // 生产环境
export function request(config={}){
    let service = axios.create({
        baseURL: baseURL
    });
    //拦截器
    service.interceptors.request.use(function (config) {
        // let token = decryptAES(localStorage.getItem('token'))
        config.headers = {
            'X-Xsrf-Token': sessionStorage.getItem('token') || ''
        }
        if (config.method==='post'){
            config.data = qs.stringify(config.data);
        }else if (config.method==='file'){
            config.method="post";
            if (config.data && config.data instanceof Object){
                let params=new FormData();
                for (let key in config.data){
                    params.append(key, config.data[key]);
                }
                config.data=params;
            }
        }
        return config;
    }, function (error) {
        return Promise.reject(error);
    });
    //响应之后
    service.interceptors.response.use(function (response) {
        if (response.data.code == '10000' || response.data.code == '300'){
            // Message({
            //     message: 'token已失效请先登录',
            //     type: 'error',
            //     duration: 1000
            // })
            router.replace('/login')
        }
        return response;
    }, function (error) {
        return Promise.reject(error);
    });
    return service.request(config).then(res=>res.data)
}